package cn.tedu.charging.order.timer;

import cn.tedu.charging.common.pojo.WebSocketMessage;
import cn.tedu.charging.order.endpoints.WebsocketServerPoint;
import cn.tedu.charging.order.mapper.BillExceptionMapper;
import cn.tedu.charging.order.mapper.BillSuccessMapper;
import cn.tedu.charging.order.pojo.po.BillExceptionPO;
import cn.tedu.charging.order.pojo.po.BillSuccessPO;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 订单检查异常结束的定时任务执行者
 */
@Component
@Slf4j
public class OrderExceptionTimer {
    @Autowired
    private BillSuccessMapper billSuccessMapper;
    @Autowired
    private BillExceptionMapper billExceptionMapper;
    @Autowired
    private WebsocketServerPoint wsPoint;
    /*
     * 检查异常订单是否结束
     * 1. 绑定jobHanlder名字 order-status-check
     * 2. 接收任务参数billId
     */
    @XxlJob("order-status-check")
    public void checkOrderStatus() {
        //1. 获取任务重携带的参数
        String billId = XxlJobContext.getXxlJobContext().getJobParam();
        log.debug("开始执行订单状态检查的任务,订单id={}",billId);
        //2. 从数据库读取成功订单信息
        BillSuccessPO success=getSuccessBill(billId);
        if (success!=null&&success.getBillStatus()==1){
            log.debug("订单状态异常");
            //3.写入异常单信息
            saveExceptionBill(success);
            //4.修改订单为异常结束
            updateBillStatus(success);
            //5.通知用户 订单异常信息
            WebSocketMessage<String> msg=new WebSocketMessage<>();
            msg.setState(1);
            msg.setMessage("订单异常结束");
            msg.setData("您的订单,充电异常,请结账走人");
            boolean pushed = wsPoint.pushMsg(success.getUserId(), JSON.toJSONString(msg));
            if (!pushed){
                log.debug("推送失败,用户ID：{},消息：{}",success.getUserId(),JSON.toJSONString(msg));
            }
        }else{
            log.debug("订单已经结束");
        }
    }

    private void updateBillStatus(BillSuccessPO success) {
        //update success set bill_status=3 where id=?
        //1.组织一个更新的参数PO 查询条件就是主键 更新非主键状态
        BillSuccessPO updatePO = new BillSuccessPO();
        updatePO.setId(success.getId());//作为where条件
        updatePO.setBillStatus(3);//作为更新的状态
        //2. 执行更新
        billSuccessMapper.updateById(updatePO);
    }

    private void saveExceptionBill(BillSuccessPO success) {
        //1. 组织异常单信息 po
        BillExceptionPO exception = new BillExceptionPO();
        exception.setBillId(success.getBillId());
        exception.setCreateTime(new Date());
        //TODO 电压 电流 温度 可以远程调用设备读取
        //2.写入到数据库
        billExceptionMapper.insert(exception);

    }

    private BillSuccessPO getSuccessBill(String billId) {
        //select * from charging_bill_success where bill_id=?
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id",billId);
        return billSuccessMapper.selectOne(queryWrapper);
    }
}